package com.diyphonegadgets.freeblog.data.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.Gallery;
import android.widget.ImageView;

import com.diyphonegadgets.freeblog.data.object.Article;
import com.diyphonegadgets.freeblog.network.HttpService;
import com.diyphonegadgets.freeblog.ui.adapter.GalleryAdapter;
import com.diyphonegadgets.freeblog.utils.AsyncImageSetter;
import com.diyphonegadgets.freeblog.utils.Debug;
import com.diyphonegadgets.freeblog.utils.GalleryUpdater;



public class DataManager {

	private static final String TAG = "YAN_DEEZER_DATA_MANAGER";

	private static DataManager sInstance;
	
    private HashMap<String, Bitmap> imageCacheForArticles;
	
	private HashMap<String,  List<Integer>> cacheForGallery;


	public static DataManager getInstance() {
		if (sInstance == null) {
			sInstance = new DataManager();
		}
		return sInstance;
	}



	private DataManager() {
		imageCacheForArticles = new HashMap<String, Bitmap>();
		cacheForGallery=new HashMap<String,  List<Integer>>();
	}

	public List<Article> getArticles(Context context)
	{
		List<Article> articles = null;
		try
		{
			articles = HttpService.getArticles(context);
		}
		catch(Exception e)
		{
			Debug.debugPermanent(TAG, "Exception : getLatestArticles -> "+e.getMessage());
			e.printStackTrace();
			return null;
		}
		return articles;
	}
	



	/*************************
	 * 
	 * YAN CACHE UTILS
	 * 
	 ************************/

	public void setBitmapToImageView(String url, ImageView imageView, String id,
			HashMap<String, Bitmap> imageCache) {

		if (imageCache.containsKey(id)) {
			Bitmap bmp = imageCache.get(id);
			if (bmp != null)
				imageView.setImageBitmap(bmp);
		} else {
			new AsyncImageSetter(imageView, id, imageCache).execute(url);
		}
	}


	public HashMap<String, Bitmap> getImageCacheForArticles() {

		return imageCacheForArticles;
	}

	public void addBitmap(String imgID, Bitmap b, HashMap<String, Bitmap> imageCache) {

		imageCache.put(imgID, b);

	}
	
	
	

	public void setImageListToGallery(String link, Gallery gallery, Article article, Context c)
	{
		
		gallery.setAdapter(new GalleryAdapter(c, new ArrayList<Integer>()));
		
		if(cacheForGallery.containsKey(link))
		{
			List<Integer> imgList = cacheForGallery.get(link);
			if(imgList != null){
				gallery.setAdapter(new GalleryAdapter(c, imgList));
				gallery.setSpacing(5);
				gallery.setSelection(gallery.getCount()-1,true);
			}
			
		}
		else
		{
			new GalleryUpdater(gallery, article, c).execute(article);
		}
	}

	
	
	public void addBitmap(String imgID, Bitmap bmp)
	{
		imageCacheForArticles.put(imgID, bmp);
	}
	
	
	public void addImageList(Article mCurrentarticle,  List<Integer> imageList){
		
		
        cacheForGallery.put(mCurrentarticle.getLink(), imageList);

	}
	

}
